package zjf.util;

import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class StringUtil {
	/**
	 * 判断字符串是否为null或空字符串
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNullOrEmpty(String str) {
		boolean result = false;
		if (null == str || "".equals(str.trim())) {
			result = true;
		}
		return result;
	}
	/**
	 * 验证手机号码的格式是否正确
	 * @param mobileString
	 * @return
	 */
	public static  boolean isMobileNO(String mobileString){
		if (mobileString == null || "".equals(mobileString.trim())) {
			return false;
		}else{
			String mobileTrim = mobileString.trim();
			Pattern patternMobile = Pattern.compile("^1[3|5|8|4|7][0-9]{9}$");
			Matcher matcherMobile = patternMobile.matcher(mobileTrim);
			if(!matcherMobile.matches()){
				return false;
			}else{
				return true;
			}
		}
	}
	/**
	 * 如果i小于10，添0后生成string
	 * 
	 * @param i
	 * @return
	 */
	public static String addZreoIfLessThanTen(int i) {

		String string = "";
		int ballNum = i;
		if (ballNum < 10) {
			string = "0" + ballNum;
		} else {
			string = ballNum + "";
		}
		return string;
	}

	/**
	 * 
	 * @param string
	 * @return
	 */
	public static boolean isNotNull(String string) {
		if (null != string && !"".equals(string.trim())) {
			return true;
		}
		return false;
	}

	/**
	 * 去掉字符串中的所有的单个空格" "
	 * 
	 * @param string
	 */
	public static String replaceSpaceCharacter(String string) {
		if (null == string || "".equals(string)) {
			return "";
		}
		return string.replace(" ", "");
	}

	/**
	 * 获取小数位为6位的经纬度
	 * 
	 * @param string
	 * @return
	 */
	public static String getStringLongitudeOrLatitude(String string) {
		if (StringUtil.isNullOrEmpty(string)) {
			return "";
		}
		if (string.contains(".")) {
			String[] splitArray = string.split("\\.");
			if (splitArray[1].length() > 6) {
				String substring = splitArray[1].substring(0, 6);
				return splitArray[0] + "." + substring;
			} else {
				return string;
			}
		} else {
			return string;
		}
	}

	public static String getForSameLengthStr(String str){
		if(isNotNull(str)){
			int length = str.length();
			switch (length) {
			case 1:
				str = "     " + str ;
				break;
			case 2:
				str = "   " + str ;
				break;
			case 3:
				str = "  " + str ;
				break;
			case 4:
				str = " " + str ;
				break;
			default:
				break;
			}
			return str ;
		}
		return "";
	}

	/**检查字符串是否是合法的日期*/
	public static boolean isVailableDate(String str){
		boolean convertSuccess = true;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // yyyy/MM/dd
		try {
			format.setLenient(false);
			format.parse(str);
		} catch (Exception e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}
		return convertSuccess ;
	}
}
